package com.itcast.bulls.stock.quote.config.sharding;

import com.itcast.bulls.stock.common.utils.GlobalConstants;
import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue;
import io.shardingsphere.api.algorithm.sharding.standard.PreciseShardingAlgorithm;
import lombok.extern.log4j.Log4j2;

import java.util.Collection;

/**
 * 分表
 */
@Log4j2
public class DataSourceTableAlg implements PreciseShardingAlgorithm<Integer> {

    /**
     * 分片配置规则
     *
     * @param collection
     * @param preciseShardingValue
     * @return
     */
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Integer> preciseShardingValue) {
        log.info("Process in DataSourceTableAlg.doSharding method,collection:" + collection + ",PreciseShardingValue:" + preciseShardingValue);
        String tableName = preciseShardingValue.getLogicTableName() + "_";
        try {
            //分表规则配置，根据周期类型映射不同的表（周期类型：1分钟，5分钟，30分钟
            Integer type = preciseShardingValue.getValue();
            if (type.equals(GlobalConstants.KLINE_PERIOD_60)) {
                //1分钟
                tableName = tableName + "1";
            } else if (type.equals(GlobalConstants.KLINE_PERIOD_300)) {
                //5分钟
                tableName = tableName + "5";
            } else if (type.equals(GlobalConstants.KLINE_PERIOD_1800)) {
                tableName = tableName + "30";
            } else {
                throw new Exception("Not Match Table Rule,type:" + type);
            }
            log.info("assign tableName:" + tableName);
            return tableName;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e.getMessage());
        }
    }
}
